function Money() {
	
}

Money.getSelect = function() {
	var sel = document.createElement("select");
	var len = arguments.length;
	for (var idx = 0; idx < len; idx++) {
		var opt = document.createElement("option");
		opt.innerHTML = arguments[idx];
		
		sel.appendChild(opt);
	}
	
	return sel;
}

Money.formatDateButton = function(el, record, column, data) {
	el.innerHTML = "";
	
	var btn = document.createElement("button");
	el.appendChild(btn);
	
	var menu = Money.getSelect("Add Income", "Add Expenditure", "Overview");
	el.appendChild(menu);
	
	new YAHOO.widget.Button(btn, {
		label: data,
		type: "split",
		menu: menu
	});
}

Money.formatEditButton = function(el, record, column, data) {
	/* Maybe effecient problem */
	el.innerHTML = "";
		
	var btn = document.createElement("button");
	
	el.appendChild(btn);
	
	new YAHOO.widget.Button(btn, {
		label: "Edit",
		className: "icon-only-button edit-icon",
		onclick: {
			fn: function() { AccountHelper.showAccountRecord(data) }
		}
	});
};

Money.formatDeleteButton = function(el, record, column, data) {
	/* Maybe effecient problem */
	el.innerHTML = "";
		
	var btn = document.createElement("button");
	
	el.appendChild(btn);
	
	new YAHOO.widget.Button(btn, {
		label: "Delete",
		className: "icon-only-button remove-icon",
		onclick: {
			fn: function() { AccountHelper.removeAccountRecord(data) }
			//obj: data,
			//scope: this
		}
	});
};

Money.formatColorText = function(el, record, column, data) {
	var val = parseInt(data);
	var col = "green";
	
	if (val < 0) {
		col = "red";
	}
	el.innerHTML = ["<font color='", col, "'>", data, "</font>"].join("");
};

Money.formatRedColorText = function(el, record, column, data) {
	el.innerHTML = "<font color='red'>" + data + "</font>"
};

Money.formatGreenColorText = function(el, record, column, data) {
	el.innerHTML = "<font color='green'>" + data + "</font>"
};

Money.prototype = {
	initSystem: function() {
		YAHOO.util.Dom.addClass(document.body, "yui-skin-sam");
		
		this.addInButton = new YAHOO.widget.Button("add-in", {
			id: "add-in-button",
			className: "icon-button add-icon",
			onclick: {
				fn: function() { this.showAccountDialog("in"); },
				obj: null,
				scope: this
			}
		});
		this.addOutButton = new YAHOO.widget.Button("add-out", {
			id: "add-out-button",
			className: "icon-button add-icon",
			onclick: {
				fn: function() { this.showAccountDialog("out"); },
				obj: null,
				scope: this
			}
		});
		
		/*
		this.viewInButton = new YAHOO.widget.Button("view-in", {
			id: "view-in-button",
			className: "icon-button view-icon",
			onclick: {
				fn: function() { this.activeTab("in"); },
				obj: null,
				scope: this
			}
		});
		this.viewOutButton = new YAHOO.widget.Button("view-out", {
			id: "view-out-button",
			className: "icon-button view-icon",
			onclick: {
				fn: function() { this.activeTab("out"); },
				obj: null,
				scope: this
			}
		});
		*/
		
		this.bodyTabPanel = new YAHOO.widget.TabView("account-info", {
			id: "account-info-tab-view",
			activeIndex: 1,
			orientation: "top"
		});
		
		this.boardPanel = new YAHOO.widget.Panel("board", {
			id: "board-panel",
			x: 100,
			//fixedcenter: true,
			underlay: "shadow",
			width: "750px",
			visible: true
		});
		
		this.boardPanel.render();
		this.boardPanel.show();
		
		this.overlayManager = this.initOverlayManager();
		this.overlayManager.register(this.boardPanel);
		
		this.initComponent();
	},
	
	initOverlayManager: function() {
		var mgr = new YAHOO.widget.OverlayManager();
		return mgr;
	},
	initComponent: function() {
		var mgr = this.overlayManager
		this.inAccountDialog = this.initAccountDialog({
			templateId: "account-in-dialog-template",
			dialogId: "account-in-dialog",
			addCategoryButtonId: "add-in-catalog-button",
			addCategoryCallback: function() { ShowInAddCategoryPanel(mgr); }
		});
		
		this.outAccountDialog = this.initAccountDialog({
			templateId: "account-out-dialog-template",
			dialogId: "account-out-dialog",
			addCategoryButtonId: "add-out-catalog-button",
			addCategoryCallback: function() { ShowOutAddCategoryPanel(mgr); }
		});
		
		this.overlayManager.register([this.inAccountDialog, this.outAccountDialog]);
		
		this.initDataTable();
	},
	initAccountDialog: function(config) {
		var accountDialog = new YAHOO.widget.Dialog(config.templateId, {
			id: config.dialogId,
			width: "320px",
			modal: false,
			constraintoviewport: true,
			fixedcenter: true,
			visible: false,
			postmethod: "async",
			buttons : [{
				text:"Submit",
				isDefault: true,
				handler: function() {
					return function() {
						this.submit();
					}
				}()
			}, {
				text:"Cancel",
				handler: function() {
					return function() {
						this.hide();
					}
				}()
			}]
		});
		
		accountDialog.callback.failure = function() {
			alert("FAL");
		};
		accountDialog.callback.success = function() {
			var m = this;
			return function(req) {
				m.handleAddSuccess(req);
			}
		}.call(this);
		
		addCatalogButton = new YAHOO.widget.Button(config.addCategoryButtonId, {
			label: "Add",
			className: "icon-only-button add-icon",
			onclick: {
				fn: config.addCategoryCallback
			}
		});
		
		accountDialog.render();
		
		return accountDialog;
	},
	initDataTable: function() {
		var outColumnDefs = [
			{key: "id", sortable: true, resizeable: true, width: "32px", label: "ID"},
            {key: "date", formatter: /*Money.formatDateButton*/YAHOO.widget.DataTable.formatDate, sortable: true, resizeable: true, label: "Date"},
			{key: "category", sortable: true, resizeable: true, label: "Category"},
            {key: "amount", formatter: Money.formatRedColorText, sortable: true, resizeable: true, label: "Amount"},
            {key: "description", sortable: true, resizeable: true, label: "Description"},
            {key: "edtoper", sortable: false, resizeable: false, width: "60px", label: "Edit", formatter: Money.formatEditButton},
            {key: "deloper", sortable: false, resizeable: false, width: "60px", label: "Delete", formatter: Money.formatDeleteButton}
		];
		
		var outDataSource = new YAHOO.util.DataSource(YAHOO.util.Dom.get("out-table"));

		outDataSource.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
		outDataSource.responseSchema = {   
			fields: [{key:"id", parser: YAHOO.util.DataSource.parseNumber},
					{key: "category"},   
                    {key:"date"},   
                    {key:"amount", parser: YAHOO.util.DataSource.parseNumber},   
                    {key:"description"},
                    {key: "edtoper"},
                    {key: "deloper"}  
            ]   
		};
		
		this.outDataTable = new YAHOO.widget.DataTable("out-data-table", outColumnDefs, outDataSource, {/*caption:"Money Income"*/});
		YAHOO.util.Dom.addClass(this.outDataTable.getTableEl(), "full-width");
		
		/*********************************************/
		
		var inColumnDefs = [
			{key: "id", sortable: true, resizeable: true, width: "32px", label: "ID"},
            {key: "date", formatter: /*Money.formatDateButton*/YAHOO.widget.DataTable.formatDate, sortable: true, resizeable: true, label: "Date"},
			{key: "category", sortable: true, resizeable: true, label: "Category"},
            {key: "amount", formatter: Money.formatGreenColorText, sortable: true, resizeable: true, label: "Amount"},
            {key: "description", sortable: true, resizeable: true, label: "Description"},
            {key: "edtoper", sortable: false, resizeable: false, width: "60px", label: "Edit", formatter: Money.formatEditButton},
            {key: "deloper", sortable: false, resizeable: false, width: "60px", label: "Delete", formatter: Money.formatDeleteButton}
		];
		
		var inDataSource = new YAHOO.util.DataSource(YAHOO.util.Dom.get("in-table"));

		inDataSource.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
		inDataSource.responseSchema = {   
			fields: [{key: "id", parser: YAHOO.util.DataSource.parseNumber},
					{key: "category"},   
                    {key: "date"},   
                    {key: "amount", parser: YAHOO.util.DataSource.parseNumber},   
                    {key: "description"},
                    {key: "edtoper"},
                    {key: "deloper"}   
            ]   
		};

        this.inDataTable = new YAHOO.widget.DataTable("in-data-table", inColumnDefs, inDataSource, {/*caption:"Money Income"*/});		
	
		YAHOO.util.Dom.addClass(this.inDataTable.getTableEl(), "full-width");
		
		/*********************************************/
		
		var statsColumnDefs = [
			{key: "category", sortable: true, resizeable: true, width: "80px", label: "Category"},
			{key: "count", formatter: YAHOO.widget.DataTable.formatNumber, sortable: true, resizeable: true, label: "Count"},
            {key: "amount", formatter: Money.formatColorText, sortable: true, resizeable: true, label: "Total Amount"}
		];
		
		var statsDataSource = new YAHOO.util.DataSource(YAHOO.util.Dom.get("stats-table"));

		statsDataSource.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
		statsDataSource.responseSchema = {   
			fields: [{key: "category"},   
                    {key:"count", parser: YAHOO.util.DataSource.parseNumber},   
                    {key:"amount", parser: YAHOO.util.DataSource.parseNumber}    
            ]   
		};
		
		this.statsDataTable = new YAHOO.widget.DataTable("stats-data-table", statsColumnDefs, statsDataSource, {/*caption:"Money Income"*/});
		YAHOO.util.Dom.addClass(this.statsDataTable.getTableEl(), "full-width");		
	},
	showAccountDialog: function(type, dt) {
		if (type == "in") {
			this.inAccountDialog.show();
			this.overlayManager.bringToTop(this.inAccountDialog);	
		} else {
			this.outAccountDialog.show();
			this.overlayManager.bringToTop(this.outAccountDialog);
		}
	},
	hideAccountDialog: function(type) {
		if (type == "in") {
			this.inAccountDialog.hide();	
		} else {
			this.outAccountDialog.hide();
		}
	},
	handleAddSuccess: function(req) {
		var record = YAHOO.lang.JSON.parse(req.responseText);
		var id = record.id;
		record.edtoper = id;
		record.deloper = id;
		
		if (record.type == "in") {
			this.inDataTable.addRow(record);	
		} else {
			this.outDataTable.addRow(record);
		}
		
	}
	
};

var money = new Money();

YAHOO.util.Event.addListener(window, "load", function() { money.initSystem(); });